package cn.stronglink.asset.message.mq.dam.handle;


import cn.stronglink.asset.message.mq.dam.entity.GetAssetListParamsEntity;
import cn.stronglink.asset.message.mq.dam.entity.RackCodeEntity;
import cn.stronglink.asset.message.mq.dam.entity.ScreenCommandEntity;
import cn.stronglink.asset.message.mq.dam.message.MQMessageOfDAM;
import cn.stronglink.suite.core.util.ByteUtil;

/**
 * 资产列表页点击下一页处理
 * @author Tgb
 *
 */
public class OpenAssetListNextPageHandle extends AbstractAssetListPageHandle {
	private int pageNum = 1;
	private int pageSize = 9;
	private boolean isClickNextPage=false;
	private final static int MAX_WAIT_TIME = 5000;
	private long lastTime = 0;
	private final static short SCREEN_ID = 3;
	@Override
	public boolean isHandle(MQMessageOfDAM msg) {
		if(!msg.getActioncode().equals(ACTION_CODE)) return false;
		ScreenCommandEntity sc = (ScreenCommandEntity) msg.getAwsPostdata();
		if(sc.getCommand().equals("EEB111000303E0100100") || 
				sc.getCommand().indexOf("EEB111000303E611")==0 || 
						sc.getCommand().indexOf("EEB111000303E711")==0) {
			return true;
		}
		return false;
	}

	@Override
	protected void handleMessage(MQMessageOfDAM msg) {
		// 判断是否超时
		if(System.currentTimeMillis()-this.lastTime>MAX_WAIT_TIME) {
			this.clearDatas();
		}
		
		ScreenCommandEntity sc = (ScreenCommandEntity) msg.getAwsPostdata();
		// 判断命令是否是下一页的命令
		if(sc.getCommand().equals("EEB111000303E0100100")) {
			this.isClickNextPage=true;
		}else if (sc.getCommand().indexOf("EEB111000303E611")==0) {
			String pageNumStr  = sc.getCommand().replace("EEB111000303E611", "");
			this.pageNum = Integer.parseInt(new String(ByteUtil.hexStringToBytes(pageNumStr)).trim());
		}else if (sc.getCommand().indexOf("EEB111000303E711")==0) {
			String pageSizeStr = sc.getCommand().replace("EEB111000303E711", "");
			this.pageSize = Integer.parseInt(new String(ByteUtil.hexStringToBytes(pageSizeStr)).trim());
		}
		
		//查询数据
		if(this.pageNum>0 && this.pageSize> 0 && this.isClickNextPage) {
			RackCodeEntity rackCodeEntity = (ScreenCommandEntity) msg.getAwsPostdata();
			if(rackCodeEntity!=null) {
				System.out.println("pageNum==="+pageNum+"===="+"pageSize==="+pageSize);
				GetAssetListParamsEntity paramsEntity = new GetAssetListParamsEntity();
				paramsEntity.setRackCode(rackCodeEntity.getRackConverCode());
				paramsEntity.setPageNum(pageNum+1);
				paramsEntity.setPageSize(pageSize);
				try {
					super.sendCommandToDam(paramsEntity, SCREEN_ID);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			this.clearDatas();
		}
		this.lastTime = System.currentTimeMillis();
	}


	private void clearDatas() {
		this.pageNum=0;
		this.pageSize=0;
		this.isClickNextPage=false;
	}
}
